﻿using NfsBLL;
using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ManagerWed.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /category/
        public ActionResult Index()
        {
            ViewData["hiden_type"] = CookieHelper.GetDecodeCookie("pcnfs_type");

            return View();
        }
        #region PC模糊查询
        public JsonResult query_category(int pageindex, int pagesize)
        {
            try
            {
                CategoryBLL bll = new CategoryBLL();
                List<category> list = bll.GetCategoryBycondtion(pageindex, pagesize);
                object pagecount = bll.GetCategoryBycondtionCount();
                return Json(new { MessageCode = "0", CategoryList = list, Pagecount = (int.Parse(pagecount.ToString()) + pagesize - 1) / pagesize }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }

        }
        #endregion

        #region 新增和修改数据

        public JsonResult GetCategory(int id)
        {
            CategoryBLL bll = new CategoryBLL();
            category model = bll.GetModel(id);
            return Json(new { model = model }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult OpeaCategory()
        {
            try
            {
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                string opea = Request["opea"] != null ? Request["opea"] : "";
                string Name = Request["name"] != null ? Request["name"] : "";
                string description = Request["description"] != null ? Request["description"] : "";
                CategoryBLL bll = new CategoryBLL();
                category model = new category { id = id,name=Name,description=description,valid = 1, time = DateTime.Now };
                bool flag;
                if (opea == "add")
                {
                    flag = bll.Add(model);
                }
                else
                {
                    flag = bll.Update(model);
                }
                if (flag)
                {
                    return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }


        }
        #endregion

        #region 绑定下拉框类别
        public JsonResult GetCategoryName()
        {
            try
            {
                CategoryBLL bll = new CategoryBLL();
                List<category> list = bll.GetModelList(" valid=1");
                return Json(new { MessageCode = "0", list = list }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion

        #region 删除数据
        [HttpPost]
        public JsonResult delete()
        {
            try
            {
                AccountBLL bll = new AccountBLL();
                int id = Request["id"] != "" ? int.Parse(Request["id"]) : 0;
                if (bll.com_delelte(id, "t_category", "valid", 0))
                {
                    return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                }
                else
                {

                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }


        }
        #endregion
	}
}